<template>
  <el-dialog
    title="添加员工"
    :visible.sync="isShow"
    width="550px"
    @close="close"
  >
    <el-form ref="form" :model="formData" label-width="80px" :rules="formRules">
      <el-form-item label="姓名" style="width: 300px" prop="username">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="手机" style="width: 300px" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" style="width: 300px" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          placeholder="请选择"
          style="width: 220px"
        >
          <el-option
            v-for="item in Employeeconstant.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" style="width: 300px" prop="workNumber">
        <el-input v-model="formData.workNumber" />
      </el-form-item>
      <el-form-item label="部门" style="width: 300px" prop="departmentName">
        <el-input v-model="formData.departmentName" @focus="hFocus" />
        <div v-show="showTree" class="tree-box">
          <el-tree
            :data="list"
            :props="{ label: 'name' }"
            @node-click="hNodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" style="width: 300px">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align: center">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="btnOk">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import Employeeconstant from '@/constant/employees'
import { getDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import { addEmployee } from '@/api/employees'
export default {
  data() {
    return {
      showTree: false,
      list: [],
      Employeeconstant,
      isShow: false,
      formData: {},
      formRules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击确定按钮
    async btnOk() {
      console.log(111)
      await this.$refs.form.validate()
      await addEmployee(this.formData)
      this.$parent.total++
      this.$parent.query.page = Math.ceil(
        this.$parent.total / this.$parent.query.size
      )
      this.$parent.loadEmployee()
      this.isShow = false
      this.$message.success('添加成功')
    },
    // 点击树节点
    hNodeClick(data) {
      // 如果有子节点就不选中
      if (data.children.length) {
        return
      }
      this.formData.departmentName = data.name
      this.showTree = false
    },
    async hFocus() {
      const res = await getDepartment()
      res.data.depts.splice(0, 1)
      this.list = tranListToTreeData(res.data.depts)
      this.showTree = true
    },
    // 关闭弹框，清空表单验证和表单数据
    close() {
      this.formData = {}
      this.$refs.form.clearValidate()
    }
  }
}
</script>
